package com.mentalhealth.diary.controller;

import com.mentalhealth.diary.dto.*;
import com.mentalhealth.diary.service.AuthService;
import com.mentalhealth.diary.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器 - V1版本
 */
@RestController
@RequestMapping("/v1/auth")
public class AuthV1Controller {

    @Autowired
    private AuthService authService;

    /**
     * 发送注册验证码
     */
    @PostMapping("/register/sms")
    public Result<String> sendRegisterCode(@Validated @RequestBody SendCodeDTO sendCodeDTO) {
        authService.sendRegisterCode(sendCodeDTO);
        return Result.success("验证码已发送，请注意查收");
    }

    /**
     * 提交注册
     */
    @PostMapping("/register/submit")
    public Result<LoginVO> submitRegister(@Validated @RequestBody RegisterDTO registerDTO) {
        LoginVO loginVO = authService.submitRegister(registerDTO);
        return Result.success("欢迎加入温暖陪伴～ 你的情绪守护已就位", loginVO);
    }

    /**
     * 发送登录验证码
     */
    @PostMapping("/login/sms")
    public Result<String> sendLoginCode(@Validated @RequestBody SendCodeDTO sendCodeDTO) {
        authService.sendLoginCode(sendCodeDTO);
        return Result.success("验证码已发送，请注意查收");
    }

    /**
     * 手机号验证码登录
     */
    @PostMapping("/login/phone")
    public Result<LoginVO> phoneLogin(@Validated @RequestBody PhoneLoginDTO phoneLoginDTO) {
        LoginVO loginVO = authService.phoneLogin(phoneLoginDTO);
        return Result.success("登录成功～ 欢迎回来", loginVO);
    }

    /**
     * 第三方登录
     */
    @PostMapping("/login/third")
    public Result<LoginVO> thirdLogin(@Validated @RequestBody ThirdLoginDTO thirdLoginDTO) {
        LoginVO loginVO = authService.thirdLogin(thirdLoginDTO);
        return Result.success("登录成功～ 欢迎回来", loginVO);
    }

    /**
     * 密码登录
     */
    @PostMapping("/login/password")
    public Result<LoginVO> passwordLogin(@Validated @RequestBody PasswordLoginDTO passwordLoginDTO) {
        LoginVO loginVO = authService.passwordLogin(passwordLoginDTO);
        return Result.success("登录成功～ 欢迎回来", loginVO);
    }

    /**
     * 忘记密码
     */
    @PostMapping("/reset-password")
    public Result<LoginVO> resetPassword(@Validated @RequestBody ResetPasswordDTO resetPasswordDTO) {
        LoginVO loginVO = authService.resetPassword(resetPasswordDTO);
        return Result.success("密码重置成功，已自动登录", loginVO);
    }

    /**
     * 统一响应结果类
     */
    public static class Result<T> {
        private Integer code;
        private String message;
        private T data;

        public Result() {
        }

        public Result(Integer code, String message, T data) {
            this.code = code;
            this.message = message;
            this.data = data;
        }

        public static <T> Result<T> success(String message, T data) {
            return new Result<>(200, message, data);
        }

        public static <T> Result<T> success(T data) {
            return new Result<>(200, "success", data);
        }

        public static <T> Result<T> success(String message) {
            return new Result<>(200, message, null);
        }

        public static <T> Result<T> error(String message) {
            return new Result<>(500, message, null);
        }

        public static <T> Result<T> error(Integer code, String message) {
            return new Result<>(code, message, null);
        }

        // getter and setter
        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }
}